<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');
$cur_foot = 'mysetting';
need_login();
$pagetitle = "收货地址管理";
$act = isset($_GET['act']) ? trim($_GET['act']) : '';
$refurl = isset($_REQUEST['refurl']) ? trim($_REQUEST['refurl']) : '';
$condition = array(
	'user_id' => $login_user_id,
);
//order/check.php?id=ZTMTQ0 

$is_js_arealimit = 0;
$cityname = $city['name'];
if(isset($INI['system']['areacondlimit']) && $INI['system']['areacondlimit']==1 && $cityname !='全国')
{
	
	
	if(!empty($city['czone']))
	{
		$condition['province'] = $city['czone'];
		$condition['city'] = $cityname;
	} else {
		$condition['province'] = $cityname;
	}
		
	$is_js_arealimit = 1;
}
//order/check.php?id=ZTMTQ0 
if($_POST){
	$up_sql = "update address set `default` = 'N' where user_id = {$login_user_id}";
	DB::Query($up_sql);
			
	$id = intval($_POST['id']);	
	
	if($id > 0) {
		$upsql = "update address set province='{$_POST[provinceId]}',city='{$_POST[cityId]}',area='{$_POST[areaId]}',street='{$_POST[adinfo]}',name='{$_POST[realname]}', 
					mobile='{$_POST[mobile]}',`default`='Y' where id = {$id}";
		
		DB::Query($upsql);
	} else {
		$addr_data = array();
		$addr_data['user_id'] = $login_user_id;
		$addr_data['province'] = $_POST['provinceId'];
		$addr_data['city'] = $_POST['cityId'];
		$addr_data['area'] = $_POST['areaId'];
		$addr_data['street'] = $_POST['adinfo'];
		$addr_data['name'] = $_POST['realname'];
		$addr_data['mobile'] = $_POST['mobile'];
		$addr_data['default'] = 'Y';
		$addr_data['zipcode'] = '';
		$addr_data['create_time'] = time();
		$addr_id  = DB::Insert('address', $addr_data);
		$id = $addr_id;
	}
	if(!empty($refurl))
	{
		redirect( WEB_ROOT . $refurl.'&aid='.$id  );
	}
	
}


$add = DB::LimitQuery('address', array(
	'condition' => $condition,
	'order' => 'ORDER BY id DESC',
));


if($act == 'add')
{
	include template('account_setaddress_edit');
} else if($act == 'edit')
{
	$id = abs(intval($_GET['id']));
	
	//address
	$address_info = Table::Fetch('address',$id);
	include template('account_setaddress_edit');
} else {
	include template('account_setaddress');
}


